import pathToRegExp from 'path-to-regexp';
import {normalize, schema} from 'normalizr';
import  {message} from 'antd';
import * as AnswersAction from '../services/answer';
const termSchema=new schema.Entity('terms', {}, { idAttribute: '_id' });
const termListSchema=[termSchema];
export default {
  namespace: 'answer',
  state: {
  	termsList:[], 
  	questionsList:[],
  },
  subscriptions: {
  	 setup: function ({history, dispatch}) {
            history.listen(location => {
                if (pathToRegExp('/answer/terms').exec(location.pathname)) {
              
                    dispatch({
                        type: 'getAllTerms'
                    });
                }
            });
        }
  },
  effects: {
  	*getQuestionsByTermsId({payload},{call,put}){
  		const {data}=yield call(AnswersAction.getQuestionsByTermsId,payload);
  		if(data.success){
  			yield put({type:'getQuestionsByTermsIdSuccess'})
  		}
  	},
  	*updateOneTermById({payload},{call,put}){
  		const {data}=yield call(AnswersAction.updateOneTermById,payload);
  	},
  	*getAllTerms({payload},{call,put}){
  		const {data}=yield call(AnswersAction.getAllTerms,payload);

  		if(data.code){
  			yield put({type:'getAllTermsSuccess',payload:data.data})
  		}
  	}
  },
  reducers: {
  	getQuestionsByTermsIdSuccess(state,{payload}){

  		return {
  			...state,
  			payload
  		}
  	},
  	getAllTermsSuccess(state,{payload}){
  	
  			return{
  				...state,
  				termsList:payload
  			}
  	}
  },
};
